package org.ezplatform.office.meeting.setting.type.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ezplatform.cloud.common.log.LogService;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.meeting.info.service.ApplyRecordService;
import org.ezplatform.office.meeting.setting.type.entity.MeetingType;
import org.ezplatform.office.meeting.setting.type.service.MeetingTypeService;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api/w/meetingTypeController")
public class MeetingTypeWebApiController extends BaseController<MeetingType, String> {

	@Autowired
	private MeetingTypeService meetingTypeService;

	@Autowired
	private ApplyRecordService applyRecordService;

    @Autowired
	private LogService logService;
    
	@Override
	protected BaseService<MeetingType, String> getEntityService() {
		return this.meetingTypeService;
	}

	/**
	 * 
	 * @Title: prepareModel
	 * @Description: 初始化数据
	 * @param request
	 * @param model
	 * @param id
	 * @return void
	 * @throws
	 */
	@ModelAttribute
	public void prepareModel(HttpServletRequest request, Model model,
			@RequestParam(value = "id", required = false) String id) {
		super.initPrepareModel(request, model, id);
	}

	/**
	 * 获取列表数据(分页)
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	@RequiresPermissions({"meeting:type:03"})
    public Page getInfoPage(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		String corpId = user.getCorpId();
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		String typeName = ValidateUtils.filter(request.getParameter("typeName"));
		return meetingTypeService.findByPage(pageable, corpId, typeName,user);
    }
	
	/**
	 * 保存/编辑
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/editSave", method = RequestMethod.POST)
    public OperationResult save(@ModelAttribute("entity") MeetingType entity){
		Map failureMap = new HashMap();
		WebUser user = WebUtils.getCurrentUser();
		String id = entity.getId();
		MeetingType mt = meetingTypeService.findByProperty("typeName", entity.getTypeName(), user.getCorpId());
		if((StringUtils.isEmpty(id) && mt != null) || (StringUtils.isNotEmpty(id) && mt != null && !id.equals(mt.getId()))) {
			failureMap.put("i18NCode", "oa.meeting.tip47");
			return OperationResult.buildFailureResult("您输入的类型名称已存在");
		}
		String oleName = "";
		if(StringUtils.isNotEmpty(id)) {
        	MeetingType mt2 = this.meetingTypeService.findOne(id);
        	oleName = mt2.getTypeName();
        }
		//记录日志
  		if(StringUtils.isNotEmpty(id) && logService.checkOptLog(user.getCorpId(), "meetingLog:type:02", user.getJwtToken())) 
  			this.logService.saveModuleLog("meetingLog:type:02", oleName, "");  
  		else if(logService.checkOptLog(user.getCorpId(), "meetingLog:type:01", user.getJwtToken()))
  			this.logService.saveModuleLog("meetingLog:type:01",  entity.getTypeName(), "");
        return super.editSave(entity);
    }
	
	/**
	 * 根据id获取数据回显
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/editShow", method = RequestMethod.GET)
	public MeetingType editShow(String id) {
		MeetingType entity = meetingTypeService.findOne(id);
		return entity;
	}
	
	/**
	 * 根据id获取数据回显
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public MeetingType info(String id) {
		MeetingType entity = meetingTypeService.findOne(id);
		WebUser user = WebUtils.getCurrentUser();
		if(logService.checkOptLog(user.getCorpId(), "meetingLog:type:03", user.getJwtToken())) {
			//记录日志
	        this.logService.saveModuleLog("meetingLog:type:03", entity.getTypeName(), "");
		}
		return entity;
	}

	/**
	 * 删除功能
	 */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public OperationResult delete(HttpServletRequest request, @RequestParam("ids") String... ids) {
    	WebUser user = WebUtils.getCurrentUser();
    	boolean canLog = logService.checkOptLog(user.getCorpId(), "meetingLog:type:04", user.getJwtToken());
    	List<Map> list = applyRecordService.getApplyRecordsByMeetingTypeId(ids);
    	if(CollectionUtils.isNotEmpty(list)) {
    		return OperationResult.buildFailureResult("所选会议类型下绑定有会议无法删除");
    	}else {
    		if(canLog) {
	    		List<MeetingType> mtlist = this.meetingTypeService.findAll(ids);
	    		//记录日志
	        	if(CollectionUtils.isNotEmpty(mtlist)) {
	        		List<Map<String, String>> operateDataList = new ArrayList<Map<String, String>>();
	        		for(MeetingType mt : mtlist) {
	        			Map<String, String> operateDataMap = new HashMap<String, String>();
	    				// 操作数据
	    				operateDataMap.put("operateData", mt.getTypeName());
	    				// 操作明细
	    				operateDataMap.put("operateDetails", "");
	    				// 请求Id
	    				operateDataMap.put("requestId", null);
	    				operateDataList.add(operateDataMap);
	        		}
	        		// 记录批量日志
	    			logService.batchSaveModuleLog("meetingLog:type:04", operateDataList, request);
	        	}
    		}
    		return super.delete(ids);
    	}
    }
    
    /**
	 * 获取会议类型数据
	 * @return
	 */
    @RequestMapping(value = "/getMeetingTypes", method = RequestMethod.GET)
    public List<Map> getMeetingTypes() {
    	WebUser user = WebUtils.getCurrentUser();
		String corpId = user.getCorpId();
    	return meetingTypeService.getMeetingTypes(corpId);
    }
}
